#include "messagetable.h"
#include "sqlitemanager.h"
#include "converse/conversetextmessage.h"
#include "converse/conversephotomessage.h"
#include "../chatmanager.h"

#include <QDebug>
#include <QSqlQuery>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlRecord>

using namespace Converse;

namespace Sqlite {

MessageTable::MessageTable(QObject *parent) :
    QObject(parent)
{
}

void MessageTable::createTable()
{
    QString str = "CREATE TABLE IF NOT EXISTS Messages (msgId INTEGER PRIMARY KEY,";
    str.append("conversationId INTEGER , ");
    str.append("sendingId INTEGER,");
    str.append("createdTime INTEGER,");
    str.append("receivedTime INTEGER,");
    str.append("senderId INTEGER,");
    str.append("receivedId INTEGER,");
    str.append("msg TEXT,");
    str.append("typeMsg INTEGER,");
    str.append("isRead INTEGER,");
    str.append("isSent INTEGER)");

    QSqlQuery qry;
    qry.prepare(str );
    SqliteManager::instance()->doProcessSQLQuery(qry);
}

void MessageTable::clearTable()
{

}

void MessageTable::insertMessage(const Converse::AbstractMessage *message)
{
    QString str = QString("INSERT OR REPLACE INTO Messages (msgId,");
    str.append("conversationId, ");
    str.append("sendingId, ");
    str.append("createdTime, ");
    str.append("receivedTime, ");
    str.append("senderId, ");
    str.append("receivedId,");
    str.append("msg, ");
    str.append("typeMsg, ");
    str.append("isRead, ");
    str.append("isSent) ");
    str.append("VALUES (%1, %2, %3, %4, %5, %6 , %7, '%8', %9, %10, %11)");

    QString command = QString(str)
            .arg(message->id())
            .arg(message->conversation().id())
            .arg(message->sendingId())
            .arg(message->createdTime())
            .arg(message->receivedTime())
            .arg(message->senderId())
            .arg(message->receiverId())
            .arg(message->message())
            .arg(message->type())
            .arg(-1)
            .arg(-1);

    //qDebug() << "MessageTable::insertMessage" << command;

    QSqlQuery qry;
    qry.prepare(command);
    SqliteManager::instance()->doProcessSQLQuery(qry);
}

void MessageTable::updateMessageIsRead(const Converse::AbstractMessage *message)
{
    QString command = QString("UPDATE  Messages  SET  isRead = %1  where msgId = %2")
            .arg(1)
            .arg(message->id());

    QSqlQuery qry;
    qry.prepare(command);
    SqliteManager::instance()->doProcessSQLQuery(qry);
}

void MessageTable::updateMessageIsSent(const Converse::AbstractMessage *message)
{
    QString command = QString("UPDATE  Messages  SET  isSent = %1  where msgId = %2")
            .arg(1)
            .arg(message->id());

    QSqlQuery qry;
    qry.prepare(command);
    SqliteManager::instance()->doProcessSQLQuery(qry);
}

void MessageTable::getMessageFromLocal(const qint64 uid,
                                       const qint64 fid,
                                       const qint64 numMessage,
                                       const qint64 messageID,
                                       AbstractConversation *conversation)
{
    QString str = QString("select ");
    str.append(" m.msgId, ");
    str.append(" m.conversationId, ");
    str.append(" m.sendingId, ");
    str.append(" m.createdTime, ");
    str.append(" m.receivedTime, ");
    str.append(" m.senderId, ");
    str.append(" m.receivedId,");
    str.append(" m.msg, ");
    str.append(" m.typeMsg, ");
    str.append(" m.isRead, ");
    str.append(" m.isSent ");
    str.append(" from Messages as m, Conversation as c");
    str.append(" where c.uid = %1");
    str.append(" and c.fid = %2");
    str.append(" and c.conversationId = m.conversationId");
    str.append(" and m.msgId > %3");
    str.append(" order by receivedTime");
    str.append(" limit %4");


    QString command = QString(str)
            .arg(uid)
            .arg(fid)
            .arg(messageID)
            .arg(numMessage);

    qDebug() << "----------MessageTable::getMessageFromLocal" << command;

    QSqlQuery qry;
    qry.prepare(command);
    SqliteManager::instance()->doProcessSQLQuery(qry);

    for( int r=0; qry.next(); r++ ) {

        qint64 msgId = qry.value(0).toLongLong();
        qint64 conversationId = qry.value(1).toLongLong();
        qint64 sendingId = qry.value(2).toLongLong();
        qint64 createdTime = qry.value(3).toLongLong();
        qint64 receivedTime = qry.value(4).toLongLong();
        qint64 senderId = qry.value(5).toLongLong();
        qint64 receivedId = qry.value(6).toLongLong();
        QString msg = qry.value(7).toString();
        int typeMsg = qry.value(8).toInt();
        int isRead = qry.value(9).toInt();
        int isSent = qry.value(10).toInt();

        conversation->onGetHistoryMeessageFromLocal(msgId,
                                                    conversationId,
                                                    sendingId,
                                                    createdTime,
                                                    receivedTime,
                                                    senderId,
                                                    receivedId,
                                                    msg,
                                                    typeMsg,
                                                    isRead,
                                                    isSent);

    }
}

}
